package com.yy.permissions_general.module.system.history.controller;

import com.yy.permissions_general.common.aopSubmit.aop.NoRepeatSubmit;
import com.yy.permissions_general.module.system.aopLog.aop.MyLog;
import com.yy.permissions_general.module.system.history.entity.History;
import com.yy.permissions_general.module.system.history.service.HistoryService;
import com.yy.permissions_general.utils.ResultUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 更新历史前端控制器
 * </p>
 *
 * @author YY
 * @since 2022-02-16
 */
@RestController
@RequestMapping("/history")
public class HistoryController {

    @Resource
    private HistoryService historyService;

    /**
    * 查询列表
    * @param history
    * @param limit
    * @param page
    * @return
    */
    @MyLog("查询列表")
    @NoRepeatSubmit(key="history.list")
    @RequestMapping("/list")
    public ResultUtil list(History history,@RequestParam(value = "limit",defaultValue = "999999")int limit, @RequestParam(value = "page",defaultValue = "0")int page) {
      return ResultUtil.buildPagination(historyService.selectPageList(history,limit,page));
    }

    /**
    * 新增数据
    * @param history
    * @return
    */
    @MyLog("新增数据")
    @NoRepeatSubmit(key="history.addHistory")
    @PreAuthorize("hasPermission('/history/addHistory','history:addHistory')")
    @RequestMapping("/addHistory")
    public ResultUtil addHistory(History history) {
        historyService.addHistory(history);
        return ResultUtil.success();
    }

    /**
    * 修改数据
    * @param history
    * @return
    */
    @MyLog("修改数据")
    @NoRepeatSubmit(key="history.editHistory")
    @PreAuthorize("hasPermission('/history/editHistory','history:editHistory')")
    @RequestMapping("/editHistory")
    public ResultUtil editHistory(History history) {
        historyService.editHistory(history);
        return ResultUtil.success();
    }

    /**
    * 查看详情
    * @param id
    * @return
    */
    @MyLog("查看详情")
    @RequestMapping("/detailHistory")
    public ResultUtil detailHistory(Integer id) {
        History history = historyService.getDetailsById(id);
        return ResultUtil.success(history);
    }

    /**
    * 根据ID删除记录
    * @param id
    * @return
    */
    @MyLog("删除")
    @NoRepeatSubmit(key="history.delete")
    @PreAuthorize("hasPermission('/history/delete','history:delete')")
    @RequestMapping("/delete")
    public ResultUtil delete(Integer id) {
        historyService.deleteById(id);
        return ResultUtil.success();
    }

    /**
    * 批量删除数据
    * @param ids ID集合
    * @return
    */
    @MyLog("批量删除")
    @NoRepeatSubmit(key="history.deleteBatch")
    @PreAuthorize("hasPermission('/history/deleteBatch','history:deleteBatch')")
    @RequestMapping("/deleteBatch")
    public ResultUtil deleteBatch(@RequestParam(value = "ids[]",required = true) List<Long> ids) {
        historyService.deleteBatch(ids);
        return ResultUtil.success();
    }

}
